package me.numark.view;

import android.content.Context;
import android.graphics.Point;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.numark.R;

/**
 * Created by Evan on 15/8/4.
 */
public class PopupMoreView extends FrameLayout {

    WindowManager mWindowManager;
    Context mContext;
    int padding;
    int width;
    @Bind(R.id.share_weixin)
    public ImageView shareWeixin;

    @Bind(R.id.share_circle)
    public ImageView shareCircle;

    @Bind(R.id.share_weibo)
    public ImageView shareWeibo;

    @Bind(R.id.share_qq)
    public ImageView shareQQ;

    @Bind(R.id.share_qzone)
    public ImageView shareQZone;

    @Bind(R.id.more_delete)
    public LinearLayout moreDelete;

    @Bind(R.id.more_copy)
    public LinearLayout moreCopy;

    @Bind(R.id.more_browser)
    public LinearLayout moreBrowser;

    @Bind(R.id.more_report)
    public LinearLayout moreReport;

    @Bind(R.id.no_watch_button)
    public FrameLayout noWatchButton;

    @Bind(R.id.no_display_button)
    public FrameLayout noDisplayButton;

    @Bind(R.id.cancel_button)
    public FrameLayout cancelButton;

    @Bind(R.id.work_layout)
    public LinearLayout workLayout;

    public PopupMoreView(Context context) {
        super(context);
        mContext = context;
        View contentView = LayoutInflater.from(context).inflate(R.layout.popup_more_view, null);
        addView(contentView);
        ButterKnife.bind(this, contentView);
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        padding = mContext.getResources().getDimensionPixelSize(R.dimen.default_padding);
        Point screenSize = new Point();
        mWindowManager.getDefaultDisplay().getSize(screenSize);
        //width = screenSize.x - 2 * padding;
        width = screenSize.x;
        setFitsSystemWindows(true);
        cancelButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void show() {
        WindowManager.LayoutParams p = new WindowManager.LayoutParams();
        p.flags = WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        p.dimAmount = 0.5f;
        p.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;
        p.width = width;
        p.x = 0;
        //p.y = padding;
        p.y = 0;
       /* Animation animation = AnimationUtils.loadAnimation(getContext(),R.anim.dialog_in_anim);
        this.startAnimation(animation);*/
        mWindowManager.addView(this, p);
    }

    public void dismiss() {
        try {
            mWindowManager.removeViewImmediate(this);
           /* Animation animation = AnimationUtils.loadAnimation(getContext(),R.anim.dialog_out_anim);
            this.startAnimation(animation);*/
        } catch (Exception e) {
        } finally {
            removeAllViews();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();

        if ((event.getAction() == MotionEvent.ACTION_DOWN)
                && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
            dismiss();
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
            dismiss();
            return true;
        } else {
            return super.onTouchEvent(event);
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (getKeyDispatcherState() == null) {
                return super.dispatchKeyEvent(event);
            }

            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0) {
                KeyEvent.DispatcherState state = getKeyDispatcherState();
                if (state != null) {
                    state.startTracking(event, this);
                }
                return true;
            } else if (event.getAction() == KeyEvent.ACTION_UP) {
                KeyEvent.DispatcherState state = getKeyDispatcherState();
                if (state != null && state.isTracking(event) && !event.isCanceled()) {
                    dismiss();
                    return true;
                }
            }
            return super.dispatchKeyEvent(event);
        } else {
            return super.dispatchKeyEvent(event);
        }
    }
}
